﻿using System;
using System.Diagnostics;
using System.IO;

namespace PhiStore.Experiments.Base64
{
    class Base64Experiment : IExperiment
    {
        object critical = new object();
        Boolean completed = false;
        byte[] bytes = new byte[10 * 1024 * 1024];
        string result = "";

        public Base64Experiment()
        {
            Random rand = new Random();
            rand.NextBytes(bytes);
        }

        public bool IsComplete()
        {
            lock (critical)
            {
                return completed;
            }
        }

        public string getResults()
        {
            if (!IsComplete())
            {
                return "";
            }
            else
            {
                return result;
            }
        }

        public void Run()
        {
            MemoryStream output = new MemoryStream();
            StreamWriter sw = new StreamWriter(output);

            Stopwatch watch = new Stopwatch();
            watch.Start();
            string encoded = Convert.ToBase64String(bytes);
            watch.Stop();

            sw.Write(encoded);

            result = "Encoding took " + watch.Elapsed.TotalSeconds.ToString() + " seconds" + System.Environment.NewLine;
            result += "10 MB data, now uses " + (Convert.ToDouble(output.Length.ToString()) / (1024.0 * 1024.0)).ToString("N2") + " MB";
            
            lock (critical)
            {
                completed = true;
            }


        }
    }
}
